import api from '@/api/user'
import { removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import store from '@/store'
import { filterAsyncRoutes } from './permission'

const state = {
  token: '',
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  nickname: '',
  email: '',
  phone: '',
}

const mutations = {
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
    localStorage.setItem('introduction', introduction)
  },
  SET_NAME: (state, name) => {
    state.name = name
    localStorage.setItem('name', name)
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
    localStorage.setItem('avatar', avatar)
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
    localStorage.setItem('roles', roles)
  },
  DEL_INTRODUCTION: (state) => {
    state.introduction = ''
    localStorage.removeItem('introduction')
  },
  DEL_NAME: (state) => {
    state.name = ''
    localStorage.removeItem('name')
  },
  DEL_AVATAR: (state) => {
    state.avatar = ''
    localStorage.removeItem('avatar')
  },
  DEL_ROLES: (state) => {
    state.roles = ''
    localStorage.removeItem('roles')
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
    localStorage.setItem('nickname', nickname)
  },
  SET_EMAIL: (state, email) => {
    state.email = email
    localStorage.setItem('email', email)
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
    localStorage.setItem('phone', phone)
  },
  DEL_NICKNAME: (state) => {
    state.nickname = ''
    localStorage.removeItem('nickname')
  },
  DEL_EMAIL: (state) => {
    state.email = ''
    localStorage.removeItem('email')
  },
  DEL_PHONE: (state) => {
    state.phone = ''
    localStorage.removeItem('phone')
  },
}
const actions = {
  // 获取验证码
  captcha({ commit }, params) {
    return new Promise((resolve, reject) => {
      api.loadCaptcha(params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 用户登录
  login({ commit }, userInfo) {
    const { username, password, code, requestId } = userInfo
    return new Promise((resolve, reject) => {
      // const data = { nickname: "pitt", code: 200 };
      // commit('SET_NAME', data ? data.nickname : '未知')
      // console.log(data)
      // resolve(data)
      api.login({ username: username.trim(), password: password, code: code, requestId: requestId }).then(response => {
        if (response.code === 200) {
          const { data } = response
          // commit('SET_NAME', data ? data.nickname : '未知')
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取权限
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      commit('SET_ROLES', 'admin')
      // commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
      commit('SET_INTRODUCTION', 'I am a super administrator')
      store.dispatch('permission/generateRoutes').then(accessRoutes => {
        if (accessRoutes) {
          resolve({ code: 200 })
        } else {
          reject({ msg: '无路由' })
        }
        // resolve(accessRoutes)
      })
    })
  },
  // 获取用户信息
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      api.userinfo().then(res => {
        if (res) {
          commit('SET_NAME', res.username)
          commit('SET_AVATAR', res.userhead)
          commit('SET_NICKNAME', res.nickname)
          commit('SET_EMAIL', res.email)
          commit('SET_PHONE', res.phone)
        }else{
          const email = localStorage.getItem('email')
          commit('SET_EMAIL', email)
          const name = localStorage.getItem('name')
          commit('SET_NAME', name)
          const avatar = localStorage.getItem('avatar')
          commit('SET_AVATAR', avatar)
          const nickname = localStorage.getItem('nickname')
          commit('SET_NICKNAME', nickname)
          const phone = localStorage.getItem('phone')
          commit('SET_PHONE', phone)
        }
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取用户未读消息数量
  getUserUnread({ commit }) {
    return new Promise((resolve, reject) => {
      api.messageCount().then(res => {
        store.dispatch('settings/changeNoticTip', {
          key: 'noticeTip',
          value: (res && res.total > 0),
        })
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 用户退出
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      api.logout().then(() => {
        // 清除缓存
        localStorage.clear()
        sessionStorage.clear()
        resetRouter()
        store.dispatch('permission/delMenuList')
        store.dispatch('tagsView/delAllViews', null, { root: true })
        router.push('/login')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // delUserInfo({ commit }, res) {
  //   commit('DEL_NAME')
  //   commit('DEL_AVATAR')
  //   commit('DEL_NICKNAME')
  //   commit('DEL_EMAIL')
  //   commit('DEL_PHONE')
  // },
  async changeRoles({ commit, dispatch }, role) {
    // const { roles } = await dispatch('getInfo')

    // resetRouter()

    // const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
    // router.addRoutes(accessRoutes)

    // dispatch('tagsView/delAllViews', null, { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
